package com.sundi.ecmarket.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.dto.CategoryDto;
import com.sundi.ecmarket.dto.KeywordDto;
import com.sundi.ecmarket.dto.ResultEntity;
import com.sundi.ecmarket.entity.EcCategory;
import com.sundi.ecmarket.service.IEcCategoryService;
import com.sundi.ecmarket.utils.UploadUtils;
import com.sundi.ecmarket.vo.CategoryVo;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 类目表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@RestController
@RequestMapping("/ecCategory")
public class EcCategoryController {


    @Autowired
    IEcCategoryService iEcCategoryService;

    @Autowired
    UploadUtils uploadUtils;


    /**
     * @deprecated ec商城中商场管理里的类目管理列表
     * @author 许家宝
     * @param categoryDto
     * @return
     */
    @RequestMapping("/xujiabao/cateList")
    public ResultEntity list(CategoryDto categoryDto){

        PageHelper.startPage(categoryDto.getPageNo(),categoryDto.getPageSize());
        List<CategoryVo> ecCategories = iEcCategoryService.selectCateList(categoryDto);
        PageInfo info = new PageInfo(ecCategories);
        return ResultEntity.ok(info);
    }

    /**
     * @deprecated ec商城中商场管理里的夫类目查询
     * @author 许家宝
     * @param categoryDto
     * @return
     */
    @RequestMapping("/xujiabao/paretList")
    public ResultEntity Plist(CategoryDto categoryDto){

        List<EcCategory> ecCategories = iEcCategoryService.selectParentList(categoryDto);
        return ResultEntity.ok(ecCategories);
    }


    /**
     * @deprecated ec商城中商场管理里的类目管理逻辑删除模块
     * @author 许家宝
     * @param id
     * @return
     */
    @RequestMapping("/xujiabao/logicdel")
    public ResultEntity del(Integer id){
        System.err.println(id);
        boolean b = iEcCategoryService.deleteByCate(id);
        return ResultEntity.ok(b);
    }


    @RequestMapping("/xujiabao/add")
    @Transactional
    public ResultEntity add(CategoryDto categoryDto) throws IOException {
        System.err.println(categoryDto);
        if(categoryDto.getFiles1()!=null){
            List<String> upload = uploadUtils.upload(categoryDto.getFiles1());
            String[] images = categoryDto.getImages();
            if(images!=null){
                for (String image: images) {
                    upload.add(image);
                }
            }
            categoryDto.setFiles1(null);
            categoryDto.setIconUrl(upload.get(0));
        }

        if(categoryDto.getFiles2()!=null){
            List<String> upload = uploadUtils.upload(categoryDto.getFiles2());
            String[] images = categoryDto.getImages();
            if(images!=null){
                for (String image: images) {
                    upload.add(image);
                }
            }
            categoryDto.setFiles2(null);
            categoryDto.setPicUrl(upload.get(0));
        }


        EcCategory ecCategory = new EcCategory();
        BeanUtils.copyProperties(categoryDto,ecCategory);
        if(ecCategory.getId()!=null){
            ecCategory.setUpdateTime(new Date());
        }else{
            ecCategory.setAddTime(new Date());
        }
        iEcCategoryService.insertOrUpdate(ecCategory);

        return ResultEntity.ok();
    }

    @RequestMapping("/xujiabao/selectByIds")
    public ResultEntity selectByIds(Integer id){

        EcCategory ecCategory = iEcCategoryService.selectById(id);
        return ResultEntity.ok(ecCategory);
    }



    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity downLoadGoodsExcel(CategoryDto categoryDto) throws Exception {

        XSSFWorkbook xSSFWorkbooks = iEcCategoryService.downLoadGoodsExcel(categoryDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("类目表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }
}
